<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Vue</title>
</head>

<body>
  <div id="app">
    <h1 align="center">图书管理系统</h1>
    <div style="width:50%;margin: 0 auto;">
      作者<el-input v-model="inputAuthor" placeholder="请输入作者"></el-input>
      书籍<el-input v-model="inputbookname" placeholder="请输入书籍"></el-input>
      <br>
      <br>
      <el-button type="primary" @click="btnadd()">增加</el-button>

      <el-divider></el-divider>
      <ul>
        <li v-for="(item,index) in dataName.data">
          {{item.name}}
          <a @click="btndelAuthor(item.name)" href="#">删除</a>
          <ul>
            <li v-for="(bookItem,bookindex) in dataName.data[index].books">
              {{bookItem}}
              <a @click="btndelBook(item.name,bookItem)" href="#">删除</a>
            </li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
  <link rel="stylesheet" type="text/css" href="./assets/css/index.css">
  <script src="./assets/js/vue.js"></script>
  <script src="./assets/js/axios.min.js"></script>

  <link rel="stylesheet" type="text/css" href="./assets/css/element.css">
  <script src="./assets/js/element.js"></script>
  <!--
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
-->
  <script>
    app = new Vue({
      //声明要绑定的id
      el: '#app',
      //页面加载完毕后执行的方法（渲染前）
      created() {
      },
      //页面加载完毕后执行的方法（渲染后）
      mounted() {
        this.getdata()
      },
      //数据对象
      data: {
        inputAuthor: '',
        inputbookname: '',
        dataName: {},
        message: "asfdsaf",
      },
      //计算属性：属性名称(){} （调用时不需要加小括号）
      computed: {

      },
      //定义方法：方法名称(){}
      methods: {
        getdata() {
          axios.get('http://127.0.0.1:5000/api/getdata').then((res) => {
            this.dataName = res.data
            console.log(this.dataName);
          }).catch((error) => {
            console.log(error);
          });
        },
        //增加书籍按钮
        btnadd() {
          if (this.inputAuthor == "") {
            this.$message('请输入作者');
            return
          }
          if (this.inputbookname == "") {
            this.$message('请输入书名');
            return
          }
          axios({
            url: 'http://127.0.0.1:5000/api/addbook',
            methods: 'post',
            params: {
              "author": this.inputAuthor,
              "book": this.inputbookname,
            },
          }).then((res) => {
            this.getdata()
            this.inputAuthor = ""
            this.inputbookname = ""
            this.$message({ message: '增加成功', type: 'success' });
          }).catch((error) => {
            this.$message.error('增加失败');
          });
        },
        //删除作者按钮
        btndelAuthor(author) {
          axios({
            url: 'http://127.0.0.1:5000/api/delauthor',
            methods: 'post',
            params: {
              "author": author,
            },
          }).then((res) => {
            this.getdata()
            this.$message({ message: '删除成功', type: 'success' });
          }).catch((error) => {
            this.$message.error('删除失败');
          });
        },
        //删除书籍按钮
        btndelBook(author, book) {
          axios({
            url: 'http://127.0.0.1:5000/api/delbook',
            methods: 'post',
            params: {
              "author": author,
              "book": book,
            },
          }).then((res) => {
            this.getdata()
            this.$message({ message: '删除成功', type: 'success' });
          }).catch((error) => {
            this.$message.error('删除失败');
          });
        },
      },
    })
  </script>
</body>

</html>